package com.xianyun.book.service;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xianyun.book.api.BookServiceI;
import com.xianyun.book.command.BookAddCmdExe;
import com.xianyun.book.command.BookCheckAddCmdExe;
import com.xianyun.book.command.BookUpdateCmdExe;
import com.xianyun.book.command.query.BookSearchQryExe;
import com.xianyun.book.domain.gateway.BookGateway;
import com.xianyun.book.dto.BookAddCmd;
import com.xianyun.book.dto.BookSearchQry;
import com.xianyun.book.dto.BookUpdateCmd;
import com.xianyun.book.dto.clientobject.BookInfoCO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * 书籍服务层
 * @author ztt
 */
@Slf4j
@CatchAndLog
@Service
public class BookServiceImpl implements BookServiceI {
    @Resource
    private BookAddCmdExe bookAddCmdExe;
    @Resource
    private BookUpdateCmdExe bookUpdateCmdExe;
    @Resource
    private BookSearchQryExe bookSearchQryExe;
    @Resource
    private BookCheckAddCmdExe checkAddCmdExe;
    @Resource
    private BookGateway bookGateway;

    @Override
    public SingleResponse<BookInfoCO> getBookInfoByIsbn(String isbn) {
        return checkAddCmdExe.execute(isbn);
    }

    @Override
    public MultiResponse<BookInfoCO> searchBooksByTag(String tag) {
        return null;
    }

    @Override
    public PageResponse<BookInfoCO> searchBookListBySort(BookSearchQry bookSearchQry) {
        return bookSearchQryExe.execute(bookSearchQry);
    }


    @Override
    public MultiResponse<String> bookSearchCompletion(String searchKey) {
        List<String> searchResult = bookGateway.bookSearchCompletion(searchKey);
        if (searchResult.isEmpty()){
            return MultiResponse.buildSuccess();
        } else {
            return MultiResponse.of(searchResult);
        }
    }

    @Override
    public Response addBookOnShelves(BookAddCmd bookAddCmd) {
        return bookAddCmdExe.execute(bookAddCmd);
    }

    @Override
    public Response refreshBookInfo(BookUpdateCmd updateCmd) {
        return bookUpdateCmdExe.execute(updateCmd);
    }

}
